package com.ly.mpayment.rest.system;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.auth0.jwt.JWT;
import com.ly.mpayment.core.util.Config;
import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.MapUtil;
import com.ly.mpayment.core.util.ResultMessage;
import com.ly.mpayment.core.util.StringUtils;
import com.ly.mpayment.persist.entity.User;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.persist.service.userloginlog.IUserLoginLogService;
import com.ly.mpayment.rest.AbsRest;

import net.sf.json.JSONObject;

@Component
@Path("system/log/")
public class UserLogRest extends AbsRest{

	@Autowired
	IUserService userService;

	@Autowired
	IUserLoginLogService userLogService;

	@POST
	@Path("searchUserLog")
	public String searchUserLog(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "starttime","endtime","start","pageSize");
		if(result == null){
			String token = request.getHeader("token");
			JSONObject gson = JsonUtils.converToJson(data);
			JSONObject json = JsonUtils.converToJson(JWT.decode(token).getSubject());
			User user = this.userService.findByUserid(json.getString(Config.tokenSignAttr));
			if(user != null){
				Map<String, Object> parameter = MapUtil.getMap();;
				if(!StringUtils.isBlank(gson.getString("starttime"))){
					parameter.put(":starttime", gson.getString("starttime"));
				}
				if(!StringUtils.isBlank(gson.getString("endtime"))){
					parameter.put(":endtime", gson.getString("endtime"));
				}
				if(!StringUtils.isBlank(gson.getString("loginip"))){
					parameter.put(":loginip", gson.getString("loginip"));
				}
				if(!StringUtils.isBlank(gson.getString("username"))){
					parameter.put(":username", gson.getString("username"));
				}
				return ResultMessage.getSucceed( JsonUtils.excludes(this.userLogService.findPageLog(parameter, gson.getInt("start"),  gson.getInt("pageSize"))
						, new String[]{"useruuid","downshopinfo","","","userstatus","password","usertype","token","lastlogintime","createtime",
								"wechatid","remark","upshoppros","upshopinfos","downorderlogs","platformaccountrecords","matchingstrategies","recharges",
								"userloginlogs","uprechargerecords","downshopproducts","downrechargeflows","downprologs","upgamerecharges","errorCodesForUseruuid"
								}));
			}
			return ResultMessage.getFailt(ResultMessage.tokenError);
		}
		return result;
	}
}
